/*
@author: user
@date: 2022/6/22 18:26
@Version: 1.0
@last modifined by : 2022/6/22 18:26
@last Modifined time : 2022/6/22 18:26
*/

import React from 'react';
import { Button } from 'antd';
import utils, { getLog } from '@/utils';
import { footerBtnEnum } from '@C/base/SDrawer/constant';

const footerBtnStyleEnum = (footer) => {
  if (footer.block) {
    return {
      display: 'flex',
    };
  }
  return {
    display: 'flex',
    textAlign: footer.align?.toLocaleLowerCase(),
  };
};

const footerLeftBtnConfig = (footer = {}) => {
  if (footer.block) {
    return {
      width: '45%',
      marginRight: '10%',
    };
  }
  return {
    marginRight: footer.btnInterval || 15,
    width: footer.btnWidth,
  };
};

const footerRightBtnConfig = (footer) => {
  if (footer.block) {
    return {
      width: '45%',
    };
  }
  return {
    marginRight: 0,
    width: footer.btnWidth,
  };
};

const getFooterBtnType = (footer = 'confirm') => {
  return /submit|confirm/img.test(footer) ? footerBtnEnum[footer] : footerBtnEnum.confirm;
}
const getBtnElement = (btnEnum = {}, submit = () => getLog('确认提交'), cancel, footer = {}) => {
  return (
    <div className="base-component-super-drawer-footer-button" style={footerBtnStyleEnum(footer)}>
      <Button type="primary" style={footerLeftBtnConfig(footer)} block={footer.block} size="large" onClick={submit}>{ btnEnum.leftBtnText }</Button>
      <Button type="primary" style={footerRightBtnConfig(footer)} block={footer.block} size="large" onClick={cancel}>{ btnEnum.rightBtnText }</Button>
    </div>
  );
};

export const footerElement = (footer, onSubmit, onCancel) => {
  if (utils.notEmptyObject(footer)) {
    const { type = '' } = footer || {};
    return getBtnElement(getFooterBtnType(type.toLocaleLowerCase()), onSubmit, onCancel, footer);
  }
  if (typeof footer === 'string') {
    return getBtnElement(getFooterBtnType(footer.toLocaleLowerCase()), onSubmit, onCancel);
  }
  if (typeof footer === 'boolean') {
    return footer ? getBtnElement(getFooterBtnType(), onSubmit, onCancel) : null;
  }
  return getBtnElement(getFooterBtnType(), onSubmit, onCancel);
};
